<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="主体" prop="topic">
        <el-input v-model="dataForm.topic" placeholder="主体"></el-input>
      </el-form-item>
      <el-form-item label="图片地址" prop="pngUrl">
        <el-upload
          :action="upFileUrl"
          list-type="picture-card"
          :auto-upload="true"
          :limit="1"
          :on-success="fileSuccess"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
              >
                <i class="el-icon-download"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="访问地址" prop="forwardUrl">
        <el-input
          v-model="dataForm.forwardUrl"
          placeholder="访问地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="展示时间" prop="showDate">
        <el-date-picker
          v-model="dataForm.showDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="dataForm.orderNum"
          placeholder="显示书喜怒"
        ></el-input>
      </el-form-item>

      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      upFileUrl: window.SITE_CONFIG.base + "file/upload",
      dataForm: {
        id: 0,
        topic: "topicTest",
        pngUrl: "url1",
        forwardUrl: "url2",
        showDate: "",
        status: 1,
        orderNum: 1,
      },
      dataRule: {
        topic: [{ required: true, message: "主体不能为空", trigger: "blur" }],
        pngUrl: [
          { required: true, message: "图片地址不能为空", trigger: "blur" },
        ],
        forwardUrl: [
          { required: true, message: "访问地址不能为空", trigger: "blur" },
        ],
        showDate: [
          { required: true, message: "展示时间不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    fileSuccess(response, file, fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.dataForm.pngUrl = response.data;
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/promotion/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.topic = data.promotion.topic;
              this.dataForm.pngUrl = data.promotion.pngUrl;
              this.dataForm.forwardUrl = data.promotion.forwardUrl;
              this.dataForm.showDate = data.promotion.showDate;
              this.dataForm.status = data.promotion.status;
              this.dataForm.orderNum = data.promotion.orderNum;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/promotion/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              topic: this.dataForm.topic,
              pngUrl: this.dataForm.pngUrl,
              forwardUrl: this.dataForm.forwardUrl,
              showDate: this.dataForm.showDate,
              status: this.dataForm.status,
              orderNum: this.dataForm.orderNum,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
